package com.bwie.parkdemo.mvvm.ui.fragment;

import android.util.Log;
import android.view.View;

import com.bwie.lib_core.mvvm.base.BaseFragment;
import com.bwie.parkdemo.R;
import com.bwie.parkdemo.BR;
import com.bwie.parkdemo.adapter.DepartmentAdapter;
import com.bwie.parkdemo.databinding.FragmentDepartmentBinding;
import com.bwie.parkdemo.mvvm.model.entity.DepartmentUserEntity;
import com.bwie.parkdemo.mvvm.viewmodel.DepartmentViewModel;
import com.bwie.parkdemo.widget.DepartmentPop;
import com.chad.library.adapter.base.BaseQuickAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public class UnDepartmentFragment extends BaseFragment<FragmentDepartmentBinding,DepartmentViewModel>
        implements BaseQuickAdapter.OnItemClickListener{
    private DepartmentPop pop;
    private DepartmentAdapter adapter;
    @Override
    public int initLayout() {
        EventBus.getDefault().register(this);
        return R.layout.fragment_department;
    }

    @Override
    public void initView() {
        vm.setType(1);//设置viewmodel请求未分组接口
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public int initVariable() {
        return BR.vm;
    }

    //弹出部门pop方法
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void showPop(List<Object> datas){
        if (adapter == null){
            adapter = new DepartmentAdapter();
            adapter.setOnItemClickListener(this);
        }
        adapter.setNewData(datas);
        if (pop == null){
            pop = new DepartmentPop(getActivity());
        }
        pop.setAdapter(adapter);
        pop.show(getActivity().getWindow().getDecorView().getRootView());
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        pop.disPop();
        //调用viewmodel下对应方法发起指定部门请求
        vm.appointDepartment(position);
    }
}
